﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using UtilZ.Dotnet.Ex.DataStruct;

namespace ConAppCore.Test
{
    unsafe internal class TestValueTypeDataMerger
    {
        public static void Test()
        {
            //Test1();
            //Test2();
            //Test3();

            //TestData();
            TestData2();
        }


        private static void TestData2()
        {
            var option = new ValueTypeDataMergerOptions()
            {
                FullMode = ContainerFullMode.DropOld,
                LimitCapcity = 25600000,
            };
            using ValueTypeDataMerger<byte> merger = new ValueTypeDataMerger<byte>(option);
            byte[] buffer = new byte[25600000];
            byte[] data = new byte[256000];

            fixed (byte* ptr = buffer)
            {
                for (int i = 0; i < 10; i++)
                {
                    merger.Append(ptr, buffer.Length);

                    while (merger.Length >= data.Length)
                    {
                        merger.Take(data, 0, data.Length);
                    }
                }
            }
        }


        private static void TestData()
        {
            var option = new ValueTypeDataMergerOptions()
            {
                FullMode = ContainerFullMode.DropOld,
                LimitCapcity = 25600000,
            };
            //using ValueTypeDataMerger<byte> merger = new ValueTypeDataMerger<byte>(option);

            //byte[] buffer = new byte[25600000];
            //byte[] data = new byte[buffer.Length];

            //fixed (byte* ptr = buffer)
            //{
            //    merger.Append(ptr, buffer.Length);
            //    merger.Take(data, 0, merger.Length);
            //}

            using ValueTypeDataMerger<int> merger = new ValueTypeDataMerger<int>(option);

            int[] buffer = new int[25600000];
            for (int i = 0; i < buffer.Length; i++)
            {
                buffer[i] = i;
            }
            int[] data = new int[buffer.Length];


            fixed (int* ptr = buffer)
            {
                merger.Append(ptr, buffer.Length);
                merger.Take(data, 0, merger.Length);
            }
        }


        private static void Test3()
        {
            var option = new ValueTypeDataMergerOptions()
            {
                FullMode = ContainerFullMode.DropOld,
                InitCapcity = 6,
                LimitCapcity = 18,
                Factor = 1.4d,
                LimitFactor = 4d
            };
            using ValueTypeDataMerger<byte> merger = new ValueTypeDataMerger<byte>(option);

            //byte[] buffer = new byte[25600000];

            byte[] data = new byte[20];

            byte[] buffer = new byte[13];
            for (byte i = 0; i < buffer.Length; i++)
            {
                buffer[i] = (byte)(i + 1);
            }

            merger.Append(new byte[] { 101 });
            fixed (byte* ptr = buffer)
            {
                merger.Append(ptr, buffer.Length);
                merger.Take(data, 0, merger.Length);
            }
        }


        private static void Test2()
        {
            var option = new ValueTypeDataMergerOptions()
            {
                FullMode = ContainerFullMode.DropOld,
                InitCapcity = 6,
                LimitCapcity = 14,
                Factor = 2d,
                LimitFactor = 4d
            };
            using ValueTypeDataMerger<byte> merger = new ValueTypeDataMerger<byte>(option);

            //byte[] buffer = new byte[25600000];

            byte[] data = new byte[20];

            byte[] buffer = new byte[13];
            for (byte i = 0; i < buffer.Length; i++)
            {
                buffer[i] = (byte)(i + 1);
            }

            merger.Append(new byte[] { 101, 102 });
            fixed (byte* ptr = buffer)
            {
                merger.Append(ptr, buffer.Length);
                merger.Take(data, 0, merger.Length);
            }
        }

        private static void Test1()
        {
            var option = new ValueTypeDataMergerOptions()
            {
                FullMode = ContainerFullMode.DropOld,
                InitCapcity = 6,
                LimitCapcity = 10,
                Factor = 1.2d,
                LimitFactor = 1.5d
            };
            using ValueTypeDataMerger<byte> merger = new ValueTypeDataMerger<byte>(option);

            //byte[] buffer = new byte[25600000];

            byte[] data = new byte[20];

            byte[] buffer = new byte[12];
            for (byte i = 0; i < buffer.Length; i++)
            {
                buffer[i] = (byte)(i + 1);
            }

            fixed (byte* ptr = buffer)
            {
                merger.Append(ptr, buffer.Length);
                merger.Take(data, 0, merger.Length);
            }
        }
    }
}
